package marksys.free.com.marksys.ui.fragment.discipline;

import android.arch.core.util.Function;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;
import android.arch.lifecycle.ViewModel;
import android.os.Build;
import android.support.annotation.MainThread;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.inject.Inject;
import marksys.free.com.marksys.nettool.common.Resource;
import marksys.free.com.marksys.nettool.common.Status;
import marksys.free.com.marksys.nettool.vo.ScoreRecords;
import marksys.free.com.marksys.repo.HistoryRepo;
import marksys.free.com.marksys.ui.fragment.ObjectUtils;
import timber.log.Timber;

public class RecordDisciplineViewModel extends ViewModel {
    private HistoryRepo historyRepo;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    private final MutableLiveData<RecordDisciplineViewModel.Query> keyLiveData = new MutableLiveData<>();
    public final LiveData<Resource<List<ScoreRecords>>> historyDisciplineLiveData = Transformations.switchMap(keyLiveData, new Function<RecordDisciplineViewModel.Query, LiveData<Resource<List<ScoreRecords>>>>() {
        @Override
        public LiveData<Resource<List<ScoreRecords>>> apply(RecordDisciplineViewModel.Query query) {
            return historyRepo.history(2);
        }
    });
    public final LiveData<Resource<List<RecordDisciplineViewModel.DisciplineDayRecord>>> Disciplinedata = Transformations.map(historyDisciplineLiveData, (Resource<List<ScoreRecords>> Disciplineinput) -> {
        switch (Disciplineinput.status) {
            case ERROR:
                return Resource.error(Disciplineinput.errorCode, Disciplineinput.errorMessage, null);
            case SUCCESS:
                List<RecordDisciplineViewModel.DisciplineDayRecord> DisciplinedayRecords = new ArrayList<>();
                Map<String, Map<String, List<ScoreRecords>>> result = new LinkedHashMap<>();
                Map<String, List<ScoreRecords>> tempTimeMap;
                List<ScoreRecords> tempScoreList;
                assert Disciplineinput.data != null;
                for (ScoreRecords Disciplinerecord : Disciplineinput.data) {
                    String time = sdf.format(Disciplinerecord.time);
                    tempTimeMap = result.get(time);
                    if (tempTimeMap == null) {
                        tempTimeMap = new LinkedHashMap<>();
                        result.put(time, tempTimeMap);
                    }
                    tempScoreList = tempTimeMap.get(Disciplinerecord.className);
                    if (tempScoreList == null) {
                        tempScoreList = new ArrayList<>();
                        tempTimeMap.put(Disciplinerecord.className, tempScoreList);
                    }
                    tempScoreList.add(Disciplinerecord);
                }

                for (String k : result.keySet()) {
                    Map<String, List<ScoreRecords>> map = result.get(k);

                    DisciplineDayRecord dr = new DisciplineDayRecord(k);
                    dr.records = new ArrayList<>();
                    DisciplinedayRecords.add(dr);

                    for (String j : map.keySet()) {
                        List<ScoreRecords> scoreRecords = map.get(j);
                        DisciplineClassRecord cr = new DisciplineClassRecord(j, k);
                        cr.DisciplinescoreRecords = new ArrayList<>();
                        cr.DisciplinescoreRecords.addAll(scoreRecords);
                        dr.records.add(cr);
                    }
                }
                Timber.i("ssssss %s", DisciplinedayRecords);
                return Resource.success(DisciplinedayRecords);
            default:
                return Resource.loading(null);
        }
    });
    public final MediatorLiveData<Resource<Boolean>> loadMoreStatus = new MediatorLiveData<>();
    public final LiveData<Boolean> hasMoreData;


    @Inject
    RecordDisciplineViewModel(HistoryRepo historyRepo) {
        this.historyRepo = historyRepo;
        hasMoreData = historyRepo.hasMoreData();
    }

    @MainThread
    public void setQueryKey(String data) {
        RecordDisciplineViewModel.Query query = new RecordDisciplineViewModel.Query(data);
        Timber.i("query : %s", query);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Objects.equals(query, keyLiveData.getValue())) {
                return;
            }
        }
        keyLiveData.setValue(query);
    }

    @MainThread
    public void refresh() {
        keyLiveData.setValue(keyLiveData.getValue());
    }

    protected class Query {
        String Disciplinedata;

        private Query(String Disciplinedata) {
            this.Disciplinedata = Disciplinedata;
        }

        @Override
        public String toString() {
            return "Query{" +
                    "disciplinedata='" + Disciplinedata + '\'' +
                    '}';
        }

    }

    private volatile boolean isLoading;

    @MainThread
    public void loadMore() {
        if (isLoading)
            return;
        else
            isLoading = true;
        LiveData<Resource<Boolean>> loadMore = historyRepo.loadMore();
        loadMoreStatus.addSource(loadMore, resource -> {
            loadMoreStatus.setValue(resource);
            if (resource != null && resource.status != Status.LOADING) {
                loadMoreStatus.removeSource(loadMore);
                isLoading = false;
            }
        });
    }

    /**
     * 日记录
     */
    public static class DisciplineDayRecord {

        public DisciplineDayRecord() {

        }

        public DisciplineDayRecord(String Disciplinedata) {
            this.disciplinedata = Disciplinedata;
        }

        //日期
        public String disciplinedata;

        public List<DisciplineClassRecord> records;

        @Override
        public String toString() {
            return "DisciplineDayRecord{" +
                    "disciplinedata='" + disciplinedata + '\'' +
                    ", records=" + records +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            DisciplineDayRecord that = (DisciplineDayRecord) o;
            return ObjectUtils.equals(disciplinedata, that.disciplinedata) &&
                    ObjectUtils.equals(records, that.records);
        }

    }

    /**
     * 班级记录
     */
    public static class DisciplineClassRecord {

        public DisciplineClassRecord() {

        }

        public DisciplineClassRecord(String DisciplineclassName, String Disciplinetime) {
            this.DisciplineclassName = DisciplineclassName;
            this.Disciplinetime = Disciplinetime;
        }

        public String Disciplinetime;
        //名字
        public String DisciplineclassName;

        public List<ScoreRecords> DisciplinescoreRecords;

        @Override
        public String toString() {
            return "DisciplineClassRecord{" +
                    "DisciplineclassName='" + DisciplineclassName + '\'' +
                    ", DisciplinescoreRecords=" + DisciplinescoreRecords +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            DisciplineClassRecord that = (DisciplineClassRecord) o;
            return ObjectUtils.equals(Disciplinetime, that.Disciplinetime) &&
                    ObjectUtils.equals(DisciplineclassName, that.DisciplineclassName) &&
                    ObjectUtils.equals(DisciplinescoreRecords, that.DisciplinescoreRecords);
        }

    }

}

